package com.toman.Adpter;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.toman.Entity.StateSelect_Entity;
import com.toman.Interfaces.BaseUrl;
import com.toman.MyApplication;
import com.toman.R;
import com.toman.Util.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import android.os.Handler;


import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by  RemoteStart_adpter on Administrator on 2017/11/27 on 10:39.
 */

public class StateOpen_adpter extends BaseAdapter implements View.OnClickListener {
    public interface SignupListClickListener {
        public void signupClick();
    }

    private SignupListClickListener signupListClickListener;

    //上下文对象
    Context context;
    //
    Handler mHandler;
    ArrayList<StateSelect_Entity> arrayList;
    int id;
    LayoutInflater inflater;
    MyApplication myApplication;

    public StateOpen_adpter(Context context, ArrayList<StateSelect_Entity> arrayList, MyApplication myApplication, Handler handler) {
        this.context = context;
        this.arrayList = arrayList;
        this.myApplication = myApplication;
        this.mHandler = handler;
        inflater = LayoutInflater.from(context);
    }

    public void setSignupListClickListener(SignupListClickListener l) {
        signupListClickListener = l;
    }

    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return arrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        AddPackage addPackage;

        if (null == convertView) {
            addPackage = new AddPackage();
            convertView = inflater.inflate(R.layout.remotestart_item, null, false);
            addPackage.img = (ImageView) convertView.findViewById(R.id.mReImg);
            addPackage.name = (TextView) convertView.findViewById(R.id.mRename);
            addPackage.shut = (TextView) convertView.findViewById(R.id.mReshut);


            convertView.setTag(R.id.type_1, addPackage);
            convertView.setTag(R.id.type_2, position);
            convertView.setOnClickListener(this);

        } else {
            addPackage = (AddPackage) convertView.getTag(R.id.type_1);
        }
        //  addPackage.img.setBackgroundResource(arrayList.get(position).getImg());

        addPackage.name.setText(arrayList.get(position).getName());
        if (arrayList.get(position).getStatus().equals("normal")) {
            addPackage.shut.setText("正常");
            addPackage.img.setBackgroundResource(R.mipmap.img_device_normal);
            addPackage.shut.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.remote6));
        } else if (arrayList.get(position).getStatus().equals("free")) {
            addPackage.shut.setText("空转");
            addPackage.img.setBackgroundResource(R.mipmap.img_device_free);
            addPackage.shut.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.remote9));
        } else if (arrayList.get(position).getStatus().equals("fault")) {
            addPackage.shut.setText("待机");
            addPackage.img.setBackgroundResource(R.mipmap.img_device_wait);
            addPackage.shut.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.remote7));
        } else if (arrayList.get(position).getStatus().equals("Off")) {
            addPackage.shut.setText("关闭");
            addPackage.img.setBackgroundResource(R.mipmap.img_device_off);
            addPackage.shut.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.remote));
        } else if (arrayList.get(position).getStatus().equals("on")) {
            addPackage.shut.setText("预热中");
            addPackage.img.setBackgroundResource(R.mipmap.img_device_hot);
            addPackage.shut.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.remote10));
        }

//        if (arrayList.get(position).isSelect()) {
//            addPackage.select.setBackgroundResource(R.mipmap.status_ok);
//        }
//else {
//            addPackage.select.setBackgroundResource(R.mipmap.status_fail);
//
//
//        }


        return convertView;
    }

    @Override
    public void onClick(View view) {
        Message msg = mHandler.obtainMessage(1);
        mHandler.sendMessage(msg);
        int position = (int) view.getTag(R.id.type_2);
        StateSelect_Entity entity = (StateSelect_Entity) getItem(position);
        ToastUtil.print(entity.getName());
        ToastUtil.print(entity.getId() + "");
        id = entity.getId();
        AddPackage addPackage = (AddPackage) view.getTag(R.id.type_1);
        if (entity.isSelect()) {
            if (entity.getStatus().equals("on")) {
                closeDialog();
            } else {
                startDialog();
            }

        } else {
            DelDialog();
        }


    }

    private void start() {
        OkHttpUtils
                .post(BaseUrl.batchStart)
                .params("token", myApplication.getSp().getString("tokenId", ""))
                .params("beltline_ids", id + "")
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String result, Request request, @Nullable Response response) {
                        try {
                            JSONObject js = new JSONObject(result);
                            int code = js.getInt("code");
                            if (code == 1) {
                                Toast.makeText(myApplication, "" + js.getString("msg"), Toast.LENGTH_SHORT).show();
                                Message msg = mHandler.obtainMessage(1);
                                mHandler.sendMessage(msg);
                                //customerManager();

                            } else {
                                Toast.makeText(myApplication, "" + js.getString("msg"), Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    private void close() {
        OkHttpUtils
                .post(BaseUrl.batchStop)
                .params("token", myApplication.getSp().getString("tokenId", ""))
                .params("beltline_ids", id + "")
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String result, Request request, @Nullable Response response) {
                        try {
                            JSONObject js = new JSONObject(result);
                            int code = js.getInt("code");
                            if (code == 1) {
                                Toast.makeText(myApplication, "" + js.getString("msg"), Toast.LENGTH_SHORT).show();
                                Message msg = mHandler.obtainMessage(1);
                                mHandler.sendMessage(msg);
                                //customerManager();

                            } else {
                                Toast.makeText(myApplication, "" + js.getString("msg"), Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    public void startDialog() {

        //LayoutInflater是用来找layout文件夹下的xml布局文件，并且实例化

        new AlertDialog.Builder(context)
                //对话框的标题
                .setTitle("提示")
                //设定显示的View
                //对话框中的“登陆”按钮的点击事件
                .setMessage("你确定要开启预热吗？")
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        start();
                    }

                })
                .setNegativeButton("再想想", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })
                // 设置dialog是否为模态，false表示模态，true表示非模态
                .setCancelable(true)
                //对话框的创建、显示
                .create().show();

    }

    public void closeDialog() {

        //LayoutInflater是用来找layout文件夹下的xml布局文件，并且实例化

        new AlertDialog.Builder(context)
                //对话框的标题
                .setTitle("提示")
                //设定显示的View
                //对话框中的“登陆”按钮的点击事件
                .setMessage("你确定要关闭预热吗？")
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        close();
                    }

                })
                .setNegativeButton("再想想", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })
                // 设置dialog是否为模态，false表示模态，true表示非模态
                .setCancelable(true)
                //对话框的创建、显示
                .create().show();

    }

    public class AddPackage {
        ImageView img, select;
        TextView name, shut;
    }

    public void DelDialog() {
        final AlertDialog dialog = new AlertDialog.Builder(context).create();
        dialog.show();
        dialog.getWindow().setContentView(R.layout.del_layout8);

        dialog.getWindow().findViewById(R.id.mDetermine8).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

    }
}


